//
//  JsonData.m
//  GitBucket
//
//  Created by gdm on 15/11/4.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "JsonData.h"

static JsonData *jsondata = nil;

@implementation JsonData

+(instancetype)allowInitDefault
{
    if (jsondata == nil) {
        //创建对象
        jsondata = [self new];
        //数据写入内存
        //文件路径
        NSString *jsonFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"user.json"];
        //读成字典
        NSData *jsonData = [[NSData alloc]initWithContentsOfFile:jsonFilePath];
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
        
        //读取图片
        //图片保存路径
        NSString *avatarFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"avatar_image.data"];
        NSData *avatarData = [NSData dataWithContentsOfFile:avatarFilePath];
        UIImage *avatarImg = [UIImage imageWithData:avatarData];
        
        
        
        
        
        //赋值属性
        jsondata.login = jsonDict[@"login"];
        jsondata.avatar = avatarImg;
        jsondata.name = jsonDict[@"name"];
        jsondata.company = jsonDict[@"company"];
        jsondata.location = jsonDict[@"location"];
        jsondata.email = jsonDict[@"email"];
        jsondata.blog = jsonDict[@"blog"];
        
        jsondata.public_repos = [jsonDict[@"public_repos"] integerValue] ;
        jsondata.following = [jsonDict[@"following"] integerValue];
        jsondata.followers = [jsonDict[@"followers"] integerValue];
        
        jsondata.repos =[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"repos.json"]] options:0 error:nil];
        
        jsondata.starreds = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"starred.json"]] options:0 error:nil];
        
        jsondata.received_events = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"received_events_url.json"]] options:0 error:nil];
        
    }
    return jsondata;
}

//重写setting

//avatar(头像)

-(void)setAvatar:(UIImage *)avatar
{
    if ([avatar isKindOfClass:[NSNull class]]) {
        _avatar = [UIImage imageWithContentsOfFile:@"mm.png"];
    }else
    {
        _avatar = avatar;
    }
}
//login
-(void)setLogin:(NSString *)login
{
    if ([login isKindOfClass:[NSNull class]]) {
        _login = @"No Set";
    }else
    {
        _login = login;
    }
}
//name
-(void)setName:(NSString *)name
{
    if ([name isKindOfClass:[NSNull class]]) {
        _name = @"No Set";
    }else
    {
        _name = name;
    }
}
//company
-(void)setCompany:(NSString *)company
{
    
    if ([company isKindOfClass:[NSNull class]]) {
        _company = @"No Set";
    }else
    {
        _company = company;
    }
}
//location
-(void)setLocation:(NSString *)location
{
    if ([location isKindOfClass:[NSNull class]]) {
        _location = @"No Set";
    }else
    {
        _location = location;
    }
}
//email
-(void)setEmail:(NSString *)email
{
    if ([email isKindOfClass:[NSNull class]]) {
        _email = @"No Set";
    }else
    {
        _email = email;
    }
}

//blog
-(void)setBlog:(NSString *)blog
{
    if ([blog isKindOfClass:[NSNull class]]) {
        _blog = @"No Set";
    }else
    {
        _blog = blog;
    }
}



@end
